﻿namespace AMEC.PCSoftware.CommunicationProtocol.CrazyHein.FINS
{
    public enum FINS_EXCEPTION_CODE_T: UInt32
    {
        NO_ERROR                                                    = 0x00000000,
        RUNTIME_ERROR                                               = 0xFFFFFFFF,

        INCOMPLETE_RESPONSE_MESSAGE                                 = 0x00000001,
        UNKNOWN_MESSAGE_HEADER                                      = 0x00000002,

        RECEIVED_INCORRECT_HANDSHAKE_RESPONSE                       = 0x00000010,
        RECEIVED_CORRUPT_HANDSHAKE_RESPONSE                         = 0x00000011,

        UNEXPECTED_ICF_VALUE                                        = 0x00000020,

        NUM_OF_REQUEST_MEMORY_OUT_OF_RANGE                          = 0x00000030,
        RECEIVED_INCORRECT_MEMORY_AREA_ACCESS_RESPONSE              = 0x00000031,
        RECEIVED_CORRUPT_MEMORY_AREA_ACCESS_RESPONSE                = 0x00000032,

        RECEIVED_UNMATCHED_HANDSHAKE_INFO                           = 0x00000100,
        RECEIVED_UNMATCHED_COMMAND                                  = 0x00000101,

        REMOTE_STATION_DISCONNECTED                                 = 0x00001000,
    }
    
    public class FINSException : Exception
    {
        public FINS_EXCEPTION_CODE_T ExceptionCode { get; private set; }
        public Exception? RuntimeException { get; private set; }

        public FINSException(FINS_EXCEPTION_CODE_T code)
        {
            ExceptionCode = code;
        }

        public FINSException(Exception exp)
        {
            ExceptionCode = FINS_EXCEPTION_CODE_T.RUNTIME_ERROR;
            RuntimeException = exp;
        }

        public override string ToString()
        {
            if (ExceptionCode != FINS_EXCEPTION_CODE_T.RUNTIME_ERROR)
                return ExceptionCode.ToString();
            else if (RuntimeException != null)
                return RuntimeException.ToString();
            else
                return "UNDEFINED_RUNTIME_ERROR";
        }

        public override string Message
        {
            get
            {
                if (ExceptionCode != FINS_EXCEPTION_CODE_T.RUNTIME_ERROR)
                    return ExceptionCode.ToString();
                else if (RuntimeException != null)
                    return RuntimeException.Message;
                else
                    return "UNDEFINED_RUNTIME_ERROR";
            }
        }
      
    }
}
